// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   nseer_db_backup.java

package include.nseer_db;

import java.io.*;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import javax.servlet.ServletContext;

// Referenced classes of package include.nseer_db:
//			connPool

public class nseer_db_backup
{

	private Connection conn;
	private Connection conn_b;
	private Statement statement;
	private Statement statement_b;
	private connPool a;
	private ServletContext app;
	private String database;
	private connPool b;
	private String drivername;
	private String url1;
	private String url2;
	private String url1_b;
	private String url2_b;
	private String ip;
	private String masterIp;
	private String strURL1;
	private String ss;

	public nseer_db_backup(ServletContext application)
	{
		app = application;
	}

	public boolean conn(String s)
	{
		database = s;
		try
		{
			a = new connPool(database, 1, app);
			conn = a.getConnection();
			statement = conn.createStatement();
			if (app.getAttribute((new StringBuilder(String.valueOf(database))).append("sync").toString()) != null)
			{
				b = new connPool(database, 1, app);
				conn_b = b.getConnection_b();
				statement_b = conn_b.createStatement();
			}
		}
		catch (SQLException sqlexception)
		{
			sqlexception.printStackTrace();
			return false;
		}
		return true;
	}

	public void getProperty()
	{
		Properties properties = new Properties();
		Properties properties_b = new Properties();
		Properties properties1 = new Properties();
		Properties properties1_b = new Properties();
		try
		{
			InputStream inputstream = getClass().getClassLoader().getResourceAsStream("/conf/db.properties");
			InputStream inputstream_b = getClass().getClassLoader().getResourceAsStream("/conf/db_backup.properties");
			InputStream inputstream1 = getClass().getClassLoader().getResourceAsStream("/conf/dbip.properties");
			InputStream inputstream1_b = getClass().getClassLoader().getResourceAsStream("/conf/dbip_backup.properties");
			properties.load(inputstream);
			properties_b.load(inputstream_b);
			properties1.load(inputstream1);
			properties1_b.load(inputstream1_b);
			if (inputstream != null)
			{
				inputstream.close();
				inputstream_b.close();
				inputstream1.close();
				inputstream1_b.close();
			}
		}
		catch (IOException ex)
		{
			System.err.println("Open Propety File Error");
		}
		drivername = properties.getProperty("DRIVER");
		url1 = (new StringBuilder(String.valueOf(properties.getProperty("URL1")))).append(properties1.getProperty("IP")).append(":3306/").toString();
		url2 = properties.getProperty("URL2");
		url1_b = (new StringBuilder(String.valueOf(properties_b.getProperty("URL1")))).append(properties1_b.getProperty("IP")).append(":3306/").toString();
		url2_b = properties_b.getProperty("URL2");
		ip = properties1_b.getProperty("IP");
		masterIp = properties1.getProperty("IP");
	}

	public ResultSet executeQuery(String sql)
	{
		ResultSet resultset = null;
		try
		{
			resultset = statement.executeQuery(sql);
		}
		catch (SQLException sqlexception)
		{
			sqlexception.printStackTrace();
		}
		return resultset;
	}

	public void executeUpdate(String sql)
	{
		try
		{
			statement.executeUpdate(sql);
			if (app.getAttribute((new StringBuilder(String.valueOf(database))).append("sync").toString()) != null)
				statement_b.executeUpdate(sql);
		}
		catch (SQLException sqlexception)
		{
			sqlexception.printStackTrace();
		}
	}

	public void close()
	{
		a.release(conn);
		if (app.getAttribute((new StringBuilder(String.valueOf(database))).append("sync").toString()) != null)
			b.release_b(conn_b);
	}

	public void setProperty(String dsm, String dss)
	{
		Properties properties = new Properties();
		Properties properties_b = new Properties();
		String strURL1 = getClass().getClassLoader().getResource("/").getPath();
		try
		{
			if (!dsm.equals(""))
			{
				String file1 = (new StringBuilder(String.valueOf(strURL1))).append("conf/dbip.properties").toString();
				FileOutputStream fos = new FileOutputStream(file1);
				properties.setProperty("IP", dsm);
				properties.store(fos, "dbip.properties");
				if (fos != null)
					fos.close();
			}
			if (!dss.equals(""))
			{
				String file2 = (new StringBuilder(String.valueOf(strURL1))).append("conf/dbip_backup.properties").toString();
				FileOutputStream fos_b = new FileOutputStream(file2);
				properties_b.setProperty("IP", dss);
				properties_b.store(fos_b, "dbip_backup.properties");
				if (fos_b != null)
					fos_b.close();
			}
		}
		catch (IOException _ex)
		{
			System.err.println("Open Propety File Error!");
		}
	}

	public String getIp()
	{
		getProperty();
		return ip;
	}

	public String getMasterIp()
	{
		getProperty();
		return masterIp;
	}

	public String getTable()
	{
		return database;
	}

	public void setTable(String s)
	{
		database = s;
	}
}
